package com.framework.boc.statistics.controller;


import com.alibaba.fastjson.JSONObject;
import com.framework.boc.response.CommonResponse;
import com.framework.boc.statistics.entity.StatisticalRecord;
import com.framework.boc.statistics.service.impl.StatisticalRecordServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.framework.boc.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 统计记录表 前端控制器
 * </p>
 *
 * @author Leil
 * @since 2020-12-23
 */
@RestController
@RequestMapping("/statistics/statistical-record")
public class StatisticalRecordController extends BaseController {

    @Autowired
    StatisticalRecordServiceImpl statisticalRecordService;

    @PostMapping("/save-records")
    public CommonResponse saveRecords(@RequestBody List<StatisticalRecord> records) {
        boolean bool = this.statisticalRecordService.saveOrUpdateBatch(records);
        return bool ? CommonResponse.success("保存成功") : CommonResponse.error("保存失败");
    }

    @GetMapping("/records")
    public CommonResponse getRecords(@RequestParam("batchId") Integer batchId) {
        List<JSONObject> records = this.statisticalRecordService.getRecords(batchId);
        return CommonResponse.success(records);
    }

    @GetMapping("/delete-record")
    public CommonResponse deleteRecord(@RequestParam("id") Integer id) {
        boolean bool = this.statisticalRecordService.removeById(id);
        return bool ? CommonResponse.success("删除成功") : CommonResponse.error("删除失败");
    }

    @GetMapping("/export-records")
    public CommonResponse exportRecords(@RequestParam("batchId") Integer batchId,
                                     @RequestParam("templateId") Integer templateId) {
        String fileName = this.statisticalRecordService.exportRecords(batchId, templateId);
        return CommonResponse.success(fileName);
    }

}

